Skip to content

Instantly share code, notes, and snippets.

@notcome
Created October 21, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notcome/214e8bc84f32ff77cdc8b1795b6e0e62 to your computer and use it in GitHub Desktop.
Save notcome/214e8bc84f32ff77cdc8b1795b6e0e62 to your computer and use it in GitHub Desktop.
What if we write web app in Swift
//: Playground - noun: a place where people can play
import Cocoa
var str = "Hello, playground"
class Node {}
class NodeList {}
enum MutationRecord {
case attributes(MutationRecordAttributes)
case characterData(MutationRecordCharacterData)
case childList(MutationRecordChildList)
}
struct MutationRecordAttributes {
let target: Node
let attributeName: String
let attributeNamespace: String
let oldValue: String?
}
struct MutationRecordCharacterData {
let target: Node
let oldValue: String?
}
struct MutationRecordChildList {
let target: Node
let addedNodes: NodeList
let removedNodes: NodeList
let previousSibling: Node
}
struct MutationObserverInit {
struct MutationObserverInitOptions : OptionSetType {
let rawValue: Int
init (rawValue: Int) {
self.rawValue = rawValue
}
/// Observe additions and removals of the target node’s child elements (including text nodes).
static let childList = MutationObserverInitOptions(rawValue: 1)
/// Observe mutations to target’s attributes.
static let attributes = MutationObserverInitOptions(rawValue: 2)
/// Observe mutations to target’s data.
static let characterData = MutationObserverInitOptions(rawValue: 4)
/// Observe not just target, but also target’s descendants.
static let subtree = MutationObserverInitOptions(rawValue: 8)
/// Record target’s attribute value before the mutation (requires `attributes` to be set).
static let attributeOldValue = MutationObserverInitOptions(rawValue: 16)
/// Record target’s data before the mutation (requires `characterData` to be set).
static let characterDataOldValue = MutationObserverInitOptions(rawValue: 32)
}
let options: MutationObserverInitOptions
let attributeFilter: [String]
init (options: MutationObserverInitOptions, attributeFilter: [String] = []) {
self.options = options
self.attributeFilter = attributeFilter
}
}
let observerOption = MutationObserverInit(options: [.characterData, .childList, .subtree])
class MutationObserver {
typealias Callback = ([MutationRecord], MutationObserver) -> Void
let callback: Callback
init (callback: Callback) {
self.callback = callback
}
/// Registers the `MutationObserver` instance to receive notifications of DOM mutations on the specified node.
/// - parameters:
/// - target: The `Node` on which to observe DOM mutations.
/// - options: A `MutationObserverInit` object, specifies which DOM mutations should be reported.
func observe (target target: Node, options: MutationObserverInit) {}
/// Stops the `MutationObserver` instance from receiving notifications of DOM mutations. Until the `observe` method is used again, observer's callback will not be invoked.
func disconnect () {}
/// Empties the `MutationObserver` instance’s record queue and returns what was in there.
func takeRecords() -> [MutationRecord] { return [] }
}
let observer = MutationObserver { _, _ in }
observer.observe(target: Node(), options: MutationObserverInit(options: [.attributeOldValue, .childList]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment