From 160eab8ccf2fe3208e8a3541bbcf8f3e2c7cdf74 Mon Sep 17 00:00:00 2001 From: anto Date: Sat, 26 Sep 2020 17:48:45 +0200 Subject: [PATCH 1/3] update --- extra.mjs | 42 ++++++++++------------------ framework.mjs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ observe.mjs | 20 +++++++++---- reactive.mjs | 9 ++++++ readable.mjs | 8 ++++++ test.mjs | 17 ++++++------ tests/run.mjs | 29 +++++++++++-------- 7 files changed, 149 insertions(+), 53 deletions(-) create mode 100644 framework.mjs diff --git a/extra.mjs b/extra.mjs index c231999..80b6412 100644 --- a/extra.mjs +++ b/extra.mjs @@ -1,33 +1,23 @@ import reactive from "./reactive.mjs" import readable from "./readable.mjs" -import observe from "./observe.mjs" +import { + Observe, + iObserve +} from "./observe.mjs" import { fullpipe } from "./utils.mjs" - import { constant, pipe, isUndefined, noop } from "./operation.mjs" -import { - injectProperties -} from "./tools.mjs" import { DomPrinter } from "./dom.mjs" -/** - * Base class for controllers, models and Views - */ -export class EventBroker { - constructor(){ - observe.call(this) - } -} - /** * it handle the data logic of the app * it takes an array of names that will be @@ -38,14 +28,13 @@ export class EventBroker { */ export function Model(self, ...props){ - class Model extends self { + class Model extends iObserve(self) { constructor(...args){ super(...args) initModel.apply(this, props) } } reactive.call(Model.prototype) - observe.call(Model.prototype) return Model; } @@ -62,11 +51,10 @@ function initModel(...props){ * @param {function} render */ -export class ViewBase extends EventBroker {} - -injectProperties.call(ViewBase.prototype, { - print: new DomPrinter() -}) +export class ViewBase extends Observe { + print = new DomPrinter() + render(){} +} export function View(render = constant("")) { class View extends ViewBase { @@ -83,7 +71,7 @@ export function View(render = constant("")) { * calling the method trigger */ const HANDLERS = Symbol('handlers'); -export class Router extends EventBroker { +export class Router extends Observe { constructor(method = "reduce", handler = reducer) { super() this[HANDLERS] = []; @@ -147,13 +135,11 @@ class Handler { */ const REGISTERED = Symbol('registered') const TRIGGER = Symbol("trigger") -export class Controller extends EventBroker { +export class Controller extends Observe { + [REGISTERED] = {} constructor(init = {}, trigger = pipe){ super() - Object.assign( - this[REGISTERED] = {}, - init - ) + this[REGISTERED] = init this[TRIGGER] = trigger; } @@ -196,7 +182,7 @@ function getter(self, p) { * @param {function} handler */ -export class Gateway extends EventBroker { +export class Gateway extends Observe { constructor(handler = fullpipe){ super() this[HANDLER] = handler diff --git a/framework.mjs b/framework.mjs new file mode 100644 index 0000000..ffdbd2e --- /dev/null +++ b/framework.mjs @@ -0,0 +1,77 @@ +import { pipe } from "./operation.mjs" + +export class Base { + constructor() { + assign.apply(this, ...arguments) + } + + static make(o = {}, ...tail){ + return new this(o, ...tail) + } +} + +export class Entity {} + +export const ENTITY_TYPE = Symbol("type") + +export class Factory extends Base { + static registered = {} + static register(e, name = e.name){ + this.registere[name] = e + } + + constructor({base = Entity, ...tail}) { + super({base, ...tail}) + } + + make(o) { + var c = this.constructor.registered[o[ENTITY_TYPE]] + if(!this.verify(c)) { + throw new IllegalArgument(`can not handle entities of type ${ + c.name || '' + }`) + } + return new c(o) + } + + verify(c){ + return Object.create(c.prototype) instanceof this.base + } + + static IllegalArgument = class extends Error {} +} + +export class Iterator extends Base { + constructor({handler = pipe, parsed = [], ...tail}){ + super({handler, parsed, ...tail}) + } + + * start(scope, ...args){ + for(var i in scope) { + if(current[Symbol.iterator] && typeof current != "string") { + for(var j of current){ + yield * this.start(j, ...args) + } + } else { + yield this.step(scope[i], ...args) + } + } + } + + step(current, ...args){ + if(current instanceof Entity && !this.parsed.find(current)){ + this.parsed.push(current) + return this.handler(current, ...args) + } + return current + } +} + +export function assign(...args){ + const proto = this.constructor.prototype + Object.assign(this, ...args) + if(proto !== Object.getPrototypeOf(this)) { + throw new TypeError("override __proto__") + } + return this +} \ No newline at end of file diff --git a/observe.mjs b/observe.mjs index eeb67bc..598f916 100644 --- a/observe.mjs +++ b/observe.mjs @@ -1,5 +1,5 @@ /** - * Observable interface + * observe interface * @module * @see module:tools */ @@ -22,7 +22,7 @@ export const OBSERVERS = Symbol("observers") /** * Interface definition - * @interface observable + * @interface observe */ const HANDLERS = { on, @@ -34,12 +34,22 @@ const HANDLERS = { /** * Interface definition - * @lends observable + * @lends observe */ -export function observable() { +export function observe() { return injectProperties.call(this, HANDLERS) } -export default observable + +export default observe + +export function iObserve(self = Object) { + class Observe extends self {} + observe.call(Observe.prototype) + return Observe +} + +export const Observe = iObserve() + /** * Builds the event list if not exist diff --git a/reactive.mjs b/reactive.mjs index ea85608..3c1ba0c 100644 --- a/reactive.mjs +++ b/reactive.mjs @@ -24,6 +24,15 @@ export default function reactive() { return injectProperties.call(this, HANDLERS) } +export function iReactive(self = Object) { + class Reactive extends self {} + reactive.call(Reactive.prototype) + return Reactive +} + +export const Reactive = iReactive() + + function buildBinder (list, build, val) { var recurring = 0 const setter = (v) => { diff --git a/readable.mjs b/readable.mjs index 1192a90..f6e49b2 100644 --- a/readable.mjs +++ b/readable.mjs @@ -36,6 +36,14 @@ export default function readable( return injectProperties.call(this, opt) } +export function iReadable(self = Object) { + class Readable extends self {} + readable.call(Readable.prototype) + return Readable +} + +export const Readable = iReadable() + function broadcast(){ this[BUFFER] = arguments flush.apply(this, arguments) diff --git a/test.mjs b/test.mjs index 37e66cc..44fc108 100644 --- a/test.mjs +++ b/test.mjs @@ -17,9 +17,13 @@ import { ASSERT_T, } from "./debug.mjs" -import observable from "./observe.mjs" +import { + iObserve + } from "./observe.mjs" -import reactive from "./reactive.mjs" +import { + iReactive +} from "./reactive.mjs" DEBUG(true) @@ -30,12 +34,12 @@ export const states = { PASSED: "PASSED" } -export class Test { - constructor(description, test = pipe, ...opt) { +export class Test extends iObserve(iReactive()) { + constructor(description, test = pipe) { good(description, String) good(test, Function) + super() this.id = ++COUNTER - this.options = opt; this.description = description; this.test = test; this.bindable("result") @@ -70,9 +74,6 @@ export class Test { Test.prototype.print = consolePrinter; -observable.call(Test.prototype) -reactive.call(Test.prototype) - export default Test; async function resolver(args, ok, ko, resolve) { diff --git a/tests/run.mjs b/tests/run.mjs index e411f17..a5ce415 100644 --- a/tests/run.mjs +++ b/tests/run.mjs @@ -4,7 +4,9 @@ import readable from "../readable.mjs"; import reactive from "../reactive.mjs"; import * as extra from "../extra.mjs"; import { - random, + Observe +} from "../observe.mjs"; +import { delay, RegObj, debounce, @@ -23,8 +25,7 @@ import { ASSERT_T, } from "../debug.mjs"; import { - View, - EventBroker + View } from "../extra.mjs"; const isBrowser = typeof Document != 'undefined' && document.body @@ -286,28 +287,32 @@ export async function testExtra() { export async function testRegObj() { return await new Test("reg obj", async () =>{ ASSERT_T("foobar!".match(new RegObj(/\w+(bar\!)/, "baz")).baz == "bar!") - }) + }).run() } -export async function testEventBroker() { - return await new Test("event broker", async () =>{ - var evt = new EventBroker() - var x = 0 +export async function testObserve() { + return await new Test("event broker", async (x = 0) =>{ + var evt = new Observe() var handler = () => ++x evt.on('one', handler) evt.on('two', handler) evt.on('three', handler) - + await delay(100) evt.fireLast('one') evt.fireLast('two') evt.fireLast('three') - + await delay(100) - + ASSERT_T(x == 1) - }) + var evt = new class extends Observe {}, y + evt.on('e', x => (y = x)) + evt.fire('e', x) + ASSERT_T(x == y) + return x + }).run() } export async function testDebounce() { From a82c8af39a959874ac4d01a64d7ff83aa3b81d48 Mon Sep 17 00:00:00 2001 From: anto Date: Sat, 26 Sep 2020 17:49:51 +0200 Subject: [PATCH 2/3] observe -> observer --- extra.mjs | 16 ++++++++-------- observe.mjs => observer.mjs | 20 ++++++++++---------- test.mjs | 8 ++++---- tests/run.mjs | 10 +++++----- 4 files changed, 27 insertions(+), 27 deletions(-) rename observe.mjs => observer.mjs (89%) diff --git a/extra.mjs b/extra.mjs index 80b6412..9a3134a 100644 --- a/extra.mjs +++ b/extra.mjs @@ -1,9 +1,9 @@ import reactive from "./reactive.mjs" import readable from "./readable.mjs" import { - Observe, - iObserve -} from "./observe.mjs" + Observer, + iObserver +} from "./observer.mjs" import { fullpipe } from "./utils.mjs" @@ -28,7 +28,7 @@ import { */ export function Model(self, ...props){ - class Model extends iObserve(self) { + class Model extends iObserver(self) { constructor(...args){ super(...args) initModel.apply(this, props) @@ -51,7 +51,7 @@ function initModel(...props){ * @param {function} render */ -export class ViewBase extends Observe { +export class ViewBase extends Observer { print = new DomPrinter() render(){} } @@ -71,7 +71,7 @@ export function View(render = constant("")) { * calling the method trigger */ const HANDLERS = Symbol('handlers'); -export class Router extends Observe { +export class Router extends Observer { constructor(method = "reduce", handler = reducer) { super() this[HANDLERS] = []; @@ -135,7 +135,7 @@ class Handler { */ const REGISTERED = Symbol('registered') const TRIGGER = Symbol("trigger") -export class Controller extends Observe { +export class Controller extends Observer { [REGISTERED] = {} constructor(init = {}, trigger = pipe){ super() @@ -182,7 +182,7 @@ function getter(self, p) { * @param {function} handler */ -export class Gateway extends Observe { +export class Gateway extends Observer { constructor(handler = fullpipe){ super() this[HANDLER] = handler diff --git a/observe.mjs b/observer.mjs similarity index 89% rename from observe.mjs rename to observer.mjs index 598f916..f2872d5 100644 --- a/observe.mjs +++ b/observer.mjs @@ -1,5 +1,5 @@ /** - * observe interface + * observer interface * @module * @see module:tools */ @@ -22,7 +22,7 @@ export const OBSERVERS = Symbol("observers") /** * Interface definition - * @interface observe + * @interface observer */ const HANDLERS = { on, @@ -34,21 +34,21 @@ const HANDLERS = { /** * Interface definition - * @lends observe + * @lends observer */ -export function observe() { +export function observer() { return injectProperties.call(this, HANDLERS) } -export default observe +export default observer -export function iObserve(self = Object) { - class Observe extends self {} - observe.call(Observe.prototype) - return Observe +export function iObserver(self = Object) { + class Observer extends self {} + observer.call(Observer.prototype) + return Observer } -export const Observe = iObserve() +export const Observer = iObserver() /** diff --git a/test.mjs b/test.mjs index 44fc108..a0057f8 100644 --- a/test.mjs +++ b/test.mjs @@ -3,7 +3,7 @@ * @module * @see module:tools * @see module:reactive - * @see module:observe + * @see module:observer */ import { @@ -18,8 +18,8 @@ import { } from "./debug.mjs" import { - iObserve - } from "./observe.mjs" + iObserver + } from "./observer.mjs" import { iReactive @@ -34,7 +34,7 @@ export const states = { PASSED: "PASSED" } -export class Test extends iObserve(iReactive()) { +export class Test extends iObserver(iReactive()) { constructor(description, test = pipe) { good(description, String) good(test, Function) diff --git a/tests/run.mjs b/tests/run.mjs index a5ce415..5001e86 100644 --- a/tests/run.mjs +++ b/tests/run.mjs @@ -4,8 +4,8 @@ import readable from "../readable.mjs"; import reactive from "../reactive.mjs"; import * as extra from "../extra.mjs"; import { - Observe -} from "../observe.mjs"; + Observer +} from "../observer.mjs"; import { delay, RegObj, @@ -291,9 +291,9 @@ export async function testRegObj() { } -export async function testObserve() { +export async function testObserver() { return await new Test("event broker", async (x = 0) =>{ - var evt = new Observe() + var evt = new Observer() var handler = () => ++x evt.on('one', handler) evt.on('two', handler) @@ -307,7 +307,7 @@ export async function testObserve() { await delay(100) ASSERT_T(x == 1) - var evt = new class extends Observe {}, y + var evt = new class extends Observer {}, y evt.on('e', x => (y = x)) evt.fire('e', x) ASSERT_T(x == y) From 30f57660900cd39cefad817b639893f0952fa38f Mon Sep 17 00:00:00 2001 From: anto Date: Thu, 15 Oct 2020 04:49:55 +0200 Subject: [PATCH 3/3] clean --- framework.mjs | 69 +++------------------------------------------------ test.mjs | 6 ++--- 2 files changed, 7 insertions(+), 68 deletions(-) diff --git a/framework.mjs b/framework.mjs index ffdbd2e..c30694b 100644 --- a/framework.mjs +++ b/framework.mjs @@ -1,71 +1,10 @@ -import { pipe } from "./operation.mjs" +import { iObserver } from "./observer.mjs" -export class Base { +export class Base extends iObserver(class { constructor() { - assign.apply(this, ...arguments) + assign.apply(this, arguments) } - - static make(o = {}, ...tail){ - return new this(o, ...tail) - } -} - -export class Entity {} - -export const ENTITY_TYPE = Symbol("type") - -export class Factory extends Base { - static registered = {} - static register(e, name = e.name){ - this.registere[name] = e - } - - constructor({base = Entity, ...tail}) { - super({base, ...tail}) - } - - make(o) { - var c = this.constructor.registered[o[ENTITY_TYPE]] - if(!this.verify(c)) { - throw new IllegalArgument(`can not handle entities of type ${ - c.name || '' - }`) - } - return new c(o) - } - - verify(c){ - return Object.create(c.prototype) instanceof this.base - } - - static IllegalArgument = class extends Error {} -} - -export class Iterator extends Base { - constructor({handler = pipe, parsed = [], ...tail}){ - super({handler, parsed, ...tail}) - } - - * start(scope, ...args){ - for(var i in scope) { - if(current[Symbol.iterator] && typeof current != "string") { - for(var j of current){ - yield * this.start(j, ...args) - } - } else { - yield this.step(scope[i], ...args) - } - } - } - - step(current, ...args){ - if(current instanceof Entity && !this.parsed.find(current)){ - this.parsed.push(current) - return this.handler(current, ...args) - } - return current - } -} +}){} export function assign(...args){ const proto = this.constructor.prototype diff --git a/test.mjs b/test.mjs index a0057f8..d4443b7 100644 --- a/test.mjs +++ b/test.mjs @@ -18,8 +18,8 @@ import { } from "./debug.mjs" import { - iObserver - } from "./observer.mjs" + Base +} from "./framework.mjs" import { iReactive @@ -34,7 +34,7 @@ export const states = { PASSED: "PASSED" } -export class Test extends iObserver(iReactive()) { +export class Test extends iReactive(Base) { constructor(description, test = pipe) { good(description, String) good(test, Function)