-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,074 additions
and
506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Options as InterpolatableOptions } from 'interpolatable'; | ||
|
||
type Unsubscribe = () => void; | ||
type Subscriber<T> = (arg: T) => void; | ||
|
||
export type Validator<Schema = unknown> = ( | ||
schema: Schema, | ||
subject: any, | ||
) => boolean; | ||
export type Resolver<Context = Record<string, unknown>> = ( | ||
object: Context, | ||
path: string, | ||
) => any; | ||
|
||
type ErrorEvent = { | ||
type: 'ValidationError' | 'PatchError'; | ||
err: Error; | ||
}; | ||
|
||
export interface JSONModifiable<Descriptor, Context> { | ||
get: () => Descriptor; | ||
setContext: (context: Context) => void; | ||
subscribe: (subscriber: Subscriber<Descriptor>) => Unsubscribe; | ||
on: (event: 'modified', subscriber: Subscriber<Descriptor>) => Unsubscribe; | ||
on: (event: 'error', subscriber: Subscriber<ErrorEvent>) => Unsubscribe; | ||
} | ||
|
||
export type Condition<Schema> = Record<string, Schema>; | ||
|
||
export type Rule<Operation, Schema = unknown, Context = unknown> = { | ||
when: Condition<Schema>[]; | ||
then?: Operation; | ||
otherwise?: Operation; | ||
options?: InterpolatableOptions<Context>; | ||
}; | ||
|
||
export type Options<Descriptor, Operation, Context> = { | ||
context?: Context; | ||
pattern?: InterpolatableOptions<Context>['pattern']; | ||
resolver?: InterpolatableOptions<Context>['resolver']; | ||
patch?: (descriptor: Descriptor, operation: Operation) => Descriptor; | ||
}; | ||
|
||
export function engine< | ||
Descriptor = Record<string, unknown>, | ||
Schema = unknown, | ||
Operation = Partial<Descriptor>, | ||
Context = unknown, | ||
>( | ||
descriptor: Descriptor, | ||
validator: Validator<Schema>, | ||
rules: Rule<Operation, Schema, Context>[], | ||
options?: Options<Descriptor, Operation, Context>, | ||
): JSONModifiable<Descriptor, Context>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createStatefulRules } from './rule'; | ||
import { compareSets } from './utils'; | ||
|
||
const resolver = (context, key) => context[key]; | ||
const patch = (...args) => Object.assign({}, ...args); | ||
|
||
export const engine = ( | ||
descriptor, | ||
validator, | ||
rules = [], | ||
{ context = {}, ...opts } = {}, | ||
subscribers = new Map(), | ||
modified, | ||
) => { | ||
opts = { resolver, patch, ...opts }; | ||
|
||
if (!validator) throw new Error(`A validator is required`); | ||
if (!opts.patch) throw new Error(`A patch function is required`); | ||
if (!opts.resolver) throw new Error(`A resolver function is required`); | ||
|
||
rules = createStatefulRules(rules, { ...opts, validator }); | ||
modified = descriptor; | ||
|
||
const emit = (eventType, thing) => ( | ||
subscribers.get(eventType)?.forEach((s) => s(thing)), thing | ||
); | ||
|
||
const on = (eventType, subscriber) => { | ||
const set = subscribers.get(eventType) || new Set(); | ||
subscribers.set(eventType, set); | ||
set.add(subscriber); | ||
return () => set.delete(subscriber); | ||
}; | ||
|
||
const evaluate = (ops) => | ||
ops.reduce((acc, op) => { | ||
try { | ||
return opts.patch(acc, op); | ||
} catch (err) { | ||
emit('error', { type: 'PatchError', err }); | ||
return acc; | ||
} | ||
}, descriptor); | ||
|
||
const cache = new Map(); | ||
const getCached = (ops) => { | ||
for (const [key, value] of cache) if (compareSets(key, ops)) return value; | ||
}; | ||
|
||
const get = () => modified; | ||
|
||
const setContext = (ctx, force) => { | ||
if (!force && ctx === context) return; | ||
const rulesToApply = rules((context = ctx)); | ||
const ops = new Set(rulesToApply); | ||
const next = getCached(ops) || evaluate(rulesToApply); | ||
modified === next || cache.set(ops, emit('modified', (modified = next))); | ||
}; | ||
|
||
const subscribe = (s) => on('modified', s); | ||
|
||
// run immediately | ||
setContext(context, true); | ||
|
||
return { on, subscribe, get, setContext }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,2 @@ | ||
import { JSONPatchOperation } from './patch'; | ||
|
||
type Unsubscribe = () => void; | ||
type Subscriber<T> = (arg: T) => void; | ||
|
||
type Validator = (schema: any, subject: any) => boolean; | ||
type Resolver = (object: Record<string, unknown>, path: string) => any; | ||
|
||
type ErrorEvent = { | ||
type: 'ValidationError' | 'PatchError'; | ||
err: Error; | ||
}; | ||
|
||
interface JSONModifiable<T, C, Op> { | ||
get: () => T; | ||
set: (descriptor: T) => void; | ||
setRules: (rules: Rule<Op>[]) => void; | ||
setContext: (context: C) => void; | ||
subscribe: (subscriber: Subscriber<T>) => Unsubscribe; | ||
on: (event: 'modified', subscriber: Subscriber<T>) => Unsubscribe; | ||
on: (event: 'error', subscriber: Subscriber<ErrorEvent>) => Unsubscribe; | ||
} | ||
|
||
type Condition = { | ||
[key: string]: Record<string, unknown>; | ||
}; | ||
|
||
type Operation = unknown; | ||
|
||
type Rule<Op> = { | ||
when: Condition[]; | ||
then?: Op[]; | ||
otherwise?: Op[]; | ||
}; | ||
|
||
type Options<T, C, Op> = { | ||
validator: Validator; | ||
context?: C; | ||
pattern?: RegExp | null; | ||
resolver?: Resolver; | ||
patch?: (operations: Op[], record: T) => T; | ||
}; | ||
|
||
export default function createJSONModifiable< | ||
T, | ||
C = Record<string, unknown>, | ||
Op = JSONPatchOperation, | ||
>( | ||
descriptor: T, | ||
rules: Rule<Op>[], | ||
options: Options<T, C, Op>, | ||
): JSONModifiable<T, C, Op>; | ||
export * from './engine'; | ||
export * from './json'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,4 @@ | ||
import defaults from './options'; | ||
import { createStatefulRules } from './rule'; | ||
import { compareSets } from './utils'; | ||
import { engine } from './engine'; | ||
import { jsonEngine } from './json'; | ||
|
||
export default ( | ||
descriptor, | ||
rules = [], | ||
{ context, ...opts } = {}, | ||
subscribers = new Map(), | ||
modified, | ||
) => { | ||
opts = { ...defaults, ...opts }; | ||
|
||
if (!opts.validator) throw new Error(`A validator is required`); | ||
|
||
rules = createStatefulRules(rules, opts); | ||
|
||
modified = descriptor; | ||
const cache = new Map(); | ||
const emit = (eventType, thing) => { | ||
const set = subscribers.get(eventType); | ||
set && set.forEach((s) => s(thing)); | ||
}; | ||
|
||
const evaluate = (ops) => | ||
ops.reduce((acc, ops) => { | ||
try { | ||
return opts.patch(acc, ops); | ||
} catch (err) { | ||
emit('error', { type: 'PatchError', err }); | ||
return acc; | ||
} | ||
}, descriptor); | ||
|
||
const getCached = (ops) => { | ||
for (const [key, value] of cache) if (compareSets(key, ops)) return value; | ||
}; | ||
|
||
const notify = (next, key) => { | ||
if (modified === next) return; | ||
cache.set(key, next); | ||
modified = next; | ||
emit('modified', modified); | ||
}; | ||
|
||
const run = () => { | ||
const rulesToApply = rules(context, opts); | ||
const ops = new Set(rulesToApply); | ||
notify(getCached(ops) || evaluate(rulesToApply), ops); | ||
}; | ||
|
||
// run immediately | ||
run(); | ||
|
||
const on = (eventType, subscriber) => { | ||
let m = subscribers.get(eventType); | ||
m | ||
? m.add(subscriber) | ||
: subscribers.set(eventType, (m = new Set([subscriber]))); | ||
|
||
return () => subscribers.get(eventType).delete(subscriber); | ||
}; | ||
|
||
return { | ||
on, | ||
subscribe: (subscriber) => on('modified', subscriber), | ||
get: () => modified, | ||
set: (d) => descriptor === d || run((descriptor = d), cache.clear()), | ||
setRules: (r) => run((rules = createStatefulRules(rules, opts))), | ||
setContext: (ctx) => run((context = ctx)), | ||
}; | ||
}; | ||
export { engine, jsonEngine }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { JSONPatchOperation } from './patch'; | ||
import { Validator, Rule, JSONModifiable, Options } from './engine'; | ||
|
||
export { JSONPatchOperation } from './patch'; | ||
|
||
type JSONOptions = Omit<Options, 'resolver' | 'patch'>; | ||
|
||
export type JSONPatchRule<Schema = unknown> = Rule< | ||
JSONPatchOperation[], | ||
Schema | ||
>; | ||
|
||
export function jsonEngine<Descriptor, Schema = unknown, Context = unknown>( | ||
descriptor: Descriptor, | ||
validator: Validator<Schema>, | ||
rules: JSONPatchRule[], | ||
options?: JSONOptions<Descriptor, JSONPatchOperation, Context>, | ||
): JSONModifiable<Descriptor, Schema, JSONPatchOperation, Context>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { patch } from './patch'; | ||
import { get } from './pointer'; | ||
import { engine } from './engine'; | ||
|
||
export const jsonEngine = (descriptor, validator, rules, opts = {}) => | ||
engine(descriptor, validator, rules, { ...opts, patch, resolver: get }); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.