Skip to content

Instantly share code, notes, and snippets.

@matthewrobb
Last active September 5, 2019 18:20
Show Gist options
  • Save matthewrobb/5d44e26d73f8ea02740721cf84c3c426 to your computer and use it in GitHub Desktop.
Save matthewrobb/5d44e26d73f8ea02740721cf84c3c426 to your computer and use it in GitHub Desktop.

Revisions

  1. matthewrobb revised this gist Sep 5, 2019. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions self-populating-list.mjs
    Original file line number Diff line number Diff line change
    @@ -9,14 +9,14 @@ export class SelfPopulatingList extends ConsumableArray {

    let count = 0;

    const tick = ()=> {
    if (this.length < 10) {
    this.push({ id: count++ });
    }
    const tick = ()=> {
    if (this.length < 10) {
    this.push({ id: count++ });
    }

    setTimeout(tick, 1000);
    };
    setTimeout(tick, 1000);
    };

    setTimeout(tick);
    setTimeout(tick);
    }
    }
  2. matthewrobb revised this gist Sep 5, 2019. No changes.
  3. matthewrobb revised this gist Sep 5, 2019. No changes.
  4. matthewrobb created this gist Sep 5, 2019.
    23 changes: 23 additions & 0 deletions array-like.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    const {
    defineProperties,
    getOwnPropertyDescriptors
    } = Object;

    /**
    * A root Array-like "class" compatible with both es5 and es6 styles
    */
    export function ArrayLike() {
    if (!(this instanceof ArrayLike)) {
    // "callable-constructor" why not?
    return new ArrayLike(...arguments);
    }

    Array.apply(this, arguments);
    }

    defineProperties(
    ArrayLike.prototype,
    getOwnPropertyDescriptors(Array.prototype)
    );

    export default ArrayLike;
    24 changes: 24 additions & 0 deletions consumable-array.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import ArrayLike from "./array-like.mjs";

    /**
    * Basic idea: iteration is consumption
    */
    export class ConsumableArray extends ArrayLike {
    [Symbol.iterator]() {
    return this;
    }

    next() {
    return this;
    }

    get value() {
    return this.shift();
    }

    get done() {
    return !this.length;
    }
    }

    export default ConsumableArray;
    22 changes: 22 additions & 0 deletions self-populating-list.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import ConsumableArray from "./consumable-array.mjs";

    /**
    * Demonstration: Will maintain a list of 10 items
    */
    export class SelfPopulatingList extends ConsumableArray {
    constructor() {
    super(...arguments);

    let count = 0;

    const tick = ()=> {
    if (this.length < 10) {
    this.push({ id: count++ });
    }

    setTimeout(tick, 1000);
    };

    setTimeout(tick);
    }
    }