Last active
August 29, 2015 14:19
-
-
Save voltrevo/a4a2ed581e06bfe18bd1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"use strict" | |
exports.container = function() { | |
var head = { | |
next: null | |
} | |
this.add = function(shell) { | |
head.next = { | |
shell: shell, | |
prev: head, | |
next: head.next | |
} | |
if (head.next.next) { | |
head.next.next.prev = head.next | |
} | |
shell.nodes.push(head.next) | |
} | |
this.forEach = function(f) { | |
var curr = head | |
while (curr.next) { | |
curr = curr.next | |
f(curr.shell.value) | |
} | |
} | |
} | |
exports.shell = function(value) { | |
var _this = this | |
this.alive = true | |
this.value = value | |
this.nodes = [] | |
this.kill = function() { | |
_this.alive = false | |
_this.value = null | |
_this.nodes.forEach(function(node) { | |
node.prev.next = node.next | |
if (node.next) { | |
node.next.prev = node.prev | |
} | |
}) | |
_this.nodes.length = 0 | |
} | |
} |
This file contains hidden or 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
"use strict" | |
var weak = require("./weak") | |
var foo = new weak.shell("foo") | |
var bar = new weak.shell("bar") | |
var firstContainer = new weak.container() | |
firstContainer.add(foo) | |
firstContainer.add(bar) | |
var secondContainer = new weak.container() | |
secondContainer.add(foo) | |
secondContainer.add(bar) | |
function printContainers() { | |
console.log("firstContainer:") | |
firstContainer.forEach(function(value) { | |
console.log(value) | |
}) | |
console.log() | |
console.log("secondContainer:") | |
secondContainer.forEach(function(value) { | |
console.log(value) | |
}) | |
console.log() | |
} | |
console.log("Initial print:") | |
printContainers() | |
console.log() | |
console.log("Kill foo") | |
foo.kill() | |
printContainers() | |
console.log() | |
console.log("Kill bar") | |
bar.kill() | |
printContainers() | |
console.log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment