Skip to content

Commit

Permalink
Update zod, rerun test, rerun dist
Browse files Browse the repository at this point in the history
  • Loading branch information
floodfx committed Jun 27, 2022
1 parent 13cb3b1 commit cc3d111
Show file tree
Hide file tree
Showing 7 changed files with 813 additions and 346 deletions.
971 changes: 740 additions & 231 deletions coverage/clover.xml

Large diffs are not rendered by default.

38 changes: 28 additions & 10 deletions coverage/coverage-final.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions dist/liveview.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ declare class LiveViewManager {

declare type Info<TInfo extends LiveInfo> = TInfo["type"] | TInfo;
/**
* Main interface to update state, interact, manage, message, and otherwise
* Main interface to update state, interact, message, and otherwise
* manage the lifecycle of a `LiveView`.
*
* The `LiveView` API (i.e. `mount`, `handleParams`, `handleInfo`, `handleEvent`)
Expand All @@ -286,7 +286,7 @@ declare type Info<TInfo extends LiveInfo> = TInfo["type"] | TInfo;
*/
interface LiveViewSocket<TContext extends LiveContext = AnyLiveContext, TInfos extends LiveInfo = AnyLiveInfo> {
/**
* The id of the `LiveView` (same as the `phx_join` id)
* The id of the `LiveView`
*/
readonly id: string;
/**
Expand All @@ -295,12 +295,12 @@ interface LiveViewSocket<TContext extends LiveContext = AnyLiveContext, TInfos e
*/
readonly connected: boolean;
/**
* The current state of the `LiveView`
* The current context (i.e. state) of the `LiveView`
*/
readonly context: TContext;
/**
* `assign` is used to update the `Context` (i.e. state) of the `LiveComponent`
* @param context you can pass a partial of the current context to update
* `assign` is used to update the context (i.e. state) of the `LiveComponent`
* @param context a `Partial` of the LiveView's context to update
*/
assign(context: Partial<TContext>): void;
/**
Expand Down Expand Up @@ -366,8 +366,8 @@ interface LiveViewSocket<TContext extends LiveContext = AnyLiveContext, TInfos e
*/
sendInfo(info: Info<TInfos>): void;
/**
* Subscribes to the given topic using pub/sub. Any events published to the topic
* will be received by the `LiveView` instance via `handleEvent`.
* Subscribes to the given topic using pub/sub. Data published to this topic
* will be received by the `LiveView` instance via `handleInfo`.
*
* @param topic the topic to subscribe this `LiveView` to
*/
Expand Down Expand Up @@ -564,7 +564,7 @@ interface LiveViewMountParams {
*/
interface LiveView<TContext extends LiveContext = AnyLiveContext, TEvents extends LiveEvent = AnyLiveEvent, TInfos extends LiveInfo = AnyLiveInfo> {
/**
* `mount` is both when the `LiveView` is rendered for the HTTP request
* `mount` is called both when the `LiveView` is rendered for the HTTP request
* and upon the first time the `LiveView` is mounted (i.e. connected) via
* the websocket. This is where you should load data and set the initial
* context of the `LiveView`.
Expand All @@ -574,7 +574,7 @@ interface LiveView<TContext extends LiveContext = AnyLiveContext, TEvents extend
*/
mount(socket: LiveViewSocket<TContext, TInfos>, session: Partial<SessionData>, params: LiveViewMountParams): void | Promise<void>;
/**
* `handleParams` is called on initial joining of the `LiveView` as well as on
* `handleParams` is called on initial loading of the `LiveView` (one-time, after `mount`) as well as on
* `pushPatch` and `livePatch` events. This is where you should handle any context (i.e. state)
* changes that are based on the `LiveView`'s URL parameters.
* @param params
Expand Down
58 changes: 14 additions & 44 deletions dist/liveview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Object.defineProperty(exports, '__esModule', { value: true });

var util = require('util');
var crypto = require('crypto');
var EventEmitter = require('events');

Expand Down Expand Up @@ -398,6 +397,8 @@ function diffArrays(oldArray, newArray) {
}
return false;
}
// Don't worry about test coverage for now since not used internally
// istanbul ignore next
function diffArrays2(oldArray, newArray) {
const diffArray = [];
// if newArray is shorter than oldArray, then we just use the newArray
Expand Down Expand Up @@ -442,6 +443,9 @@ function diffArrays2(oldArray, newArray) {
}

// Initially copied from https://github.com/Janpot/escape-html-template-tag/blob/master/src/index.ts
// This is a modified version of escape-html-template-tag that builds a tree
// of statics and dynamics that can be used to render the template.
//
const ENTITIES = {
"&": "&amp;",
"<": "&lt;",
Expand Down Expand Up @@ -488,7 +492,7 @@ class HtmlSafeString {
// statics.length should always equal dynamics.length + 1
if (this.dynamics.length === 0) {
if (this.statics.length !== 1) {
throw new Error("Expected exactly one static string for HtmlSafeString" + util.inspect(this));
throw new Error("Expected exactly one static string for HtmlSafeString" + this);
}
// TODO Optimization to just return the single static string?
// if only statics, return just the statics
Expand Down Expand Up @@ -549,7 +553,12 @@ class HtmlSafeString {
// elements of Array are either: HtmlSafeString or Promise<HtmlSafeString>
let d;
let s;
if (cur[0] instanceof HtmlSafeString) {
// istanbul ignore next
if (cur[0] instanceof Promise) {
// istanbul ignore next
throw new Error("Promise not supported in HtmlSafeString, try using Promise.all to wait for all promises to resolve.");
}
else if (cur[0] instanceof HtmlSafeString) {
// if any of the children are live components, then we assume they all are
// and do not return the statics for this array
let isLiveComponentArray = false;
Expand All @@ -575,48 +584,10 @@ class HtmlSafeString {
[`${index}`]: { d, s },
};
}
else if (cur[0] instanceof Promise) {
// collect all the dynamic partsTrees
let isLiveComponentArray = false;
d = cur.map(async (c) => {
const c2 = await c;
if (c2.isLiveComponent) {
isLiveComponentArray = true;
return [Number(c2.statics[0])];
}
else {
return Object.values(c2.partsTree(false));
}
});
if (isLiveComponentArray) {
return {
...acc,
[`${index}`]: { d },
};
}
// not an array of LiveComponents so return the statics too
// we know the statics are the same for all elements so just return the first
// element of the statics array
s = cur.map(async (c) => (await c).statics)[0];
return {
...acc,
[`${index}`]: { d, s },
};
}
else {
throw new Error("Expected HtmlSafeString or Promise<HtmlSafeString> but got: ", cur[0].constructor.name);
// istanbul ignore next
throw new Error("Expected HtmlSafeString or Promise<HtmlSafeString> but got:", cur[0].constructor.name);
}
// OLD Way - Assume all elements are the same type
// const currentPart = cur as HtmlSafeString[];
// // collect all the dynamic partsTrees
// const d = currentPart.map((c) => Object.values(c.partsTree(false)));
// // we know the statics are the same for all the children
// // so we can just take the first one
// const s = currentPart.map((c) => c.statics)[0];
// return {
// ...acc,
// [`${index}`]: { d, s },
// };
}
}
else {
Expand Down Expand Up @@ -644,7 +615,6 @@ function html(statics, ...dynamics) {
return new HtmlSafeString(statics, dynamics);
}

// TODO insert hidden input for CSRF token?
const form_for = (action, csrfToken, options) => {
const method = options?.method ?? "post";
const phx_submit = options?.phx_submit ? safe(` phx-submit="${options.phx_submit}"`) : "";
Expand Down
58 changes: 14 additions & 44 deletions dist/liveview.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

/// <reference types="./liveview.d.ts" />
import { inspect } from 'util';
import crypto from 'crypto';
import EventEmitter from 'events';

Expand Down Expand Up @@ -391,6 +390,8 @@ function diffArrays(oldArray, newArray) {
}
return false;
}
// Don't worry about test coverage for now since not used internally
// istanbul ignore next
function diffArrays2(oldArray, newArray) {
const diffArray = [];
// if newArray is shorter than oldArray, then we just use the newArray
Expand Down Expand Up @@ -435,6 +436,9 @@ function diffArrays2(oldArray, newArray) {
}

// Initially copied from https://github.com/Janpot/escape-html-template-tag/blob/master/src/index.ts
// This is a modified version of escape-html-template-tag that builds a tree
// of statics and dynamics that can be used to render the template.
//
const ENTITIES = {
"&": "&amp;",
"<": "&lt;",
Expand Down Expand Up @@ -481,7 +485,7 @@ class HtmlSafeString {
// statics.length should always equal dynamics.length + 1
if (this.dynamics.length === 0) {
if (this.statics.length !== 1) {
throw new Error("Expected exactly one static string for HtmlSafeString" + inspect(this));
throw new Error("Expected exactly one static string for HtmlSafeString" + this);
}
// TODO Optimization to just return the single static string?
// if only statics, return just the statics
Expand Down Expand Up @@ -542,7 +546,12 @@ class HtmlSafeString {
// elements of Array are either: HtmlSafeString or Promise<HtmlSafeString>
let d;
let s;
if (cur[0] instanceof HtmlSafeString) {
// istanbul ignore next
if (cur[0] instanceof Promise) {
// istanbul ignore next
throw new Error("Promise not supported in HtmlSafeString, try using Promise.all to wait for all promises to resolve.");
}
else if (cur[0] instanceof HtmlSafeString) {
// if any of the children are live components, then we assume they all are
// and do not return the statics for this array
let isLiveComponentArray = false;
Expand All @@ -568,48 +577,10 @@ class HtmlSafeString {
[`${index}`]: { d, s },
};
}
else if (cur[0] instanceof Promise) {
// collect all the dynamic partsTrees
let isLiveComponentArray = false;
d = cur.map(async (c) => {
const c2 = await c;
if (c2.isLiveComponent) {
isLiveComponentArray = true;
return [Number(c2.statics[0])];
}
else {
return Object.values(c2.partsTree(false));
}
});
if (isLiveComponentArray) {
return {
...acc,
[`${index}`]: { d },
};
}
// not an array of LiveComponents so return the statics too
// we know the statics are the same for all elements so just return the first
// element of the statics array
s = cur.map(async (c) => (await c).statics)[0];
return {
...acc,
[`${index}`]: { d, s },
};
}
else {
throw new Error("Expected HtmlSafeString or Promise<HtmlSafeString> but got: ", cur[0].constructor.name);
// istanbul ignore next
throw new Error("Expected HtmlSafeString or Promise<HtmlSafeString> but got:", cur[0].constructor.name);
}
// OLD Way - Assume all elements are the same type
// const currentPart = cur as HtmlSafeString[];
// // collect all the dynamic partsTrees
// const d = currentPart.map((c) => Object.values(c.partsTree(false)));
// // we know the statics are the same for all the children
// // so we can just take the first one
// const s = currentPart.map((c) => c.statics)[0];
// return {
// ...acc,
// [`${index}`]: { d, s },
// };
}
}
else {
Expand Down Expand Up @@ -637,7 +608,6 @@ function html(statics, ...dynamics) {
return new HtmlSafeString(statics, dynamics);
}

// TODO insert hidden input for CSRF token?
const form_for = (action, csrfToken, options) => {
const method = options?.method ?? "post";
const phx_submit = options?.phx_submit ? safe(` phx-submit="${options.phx_submit}"`) : "";
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"deep-object-diff": "^1.1.7",
"nanoid": "^3.2.0",
"zod": "^3.14.3"
"zod": "^3.17.3"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.2",
Expand Down

0 comments on commit cc3d111

Please sign in to comment.