<commit_before>define( [ "nwGui", "nwWindow", "ember" ], function( nwGui, nwWindow, Ember ) {

	var get = Ember.get,
	    set = Ember.set;

	var ismaximize = false;

	function deferEvent( fn ) {
		return function() {
			var args = arguments;
			setTimeout(function() {
				fn.apply( null, args );
			}, 10 );
		};
	}

	var timeout = null;
	function delegateSave() {
		clearTimeout( timeout );
		timeout = setTimeout( this.save.bind( this ), 100 );
	}

	function onResize( width, height ) {
		if ( ismaximize ) { return; }
		set( this, "width", width );
		set( this, "height", height );
		delegateSave.call( this );
	}

	function onMove( x, y ) {
		if ( ismaximize ) { return; }
		set( this, "x", x );
		set( this, "y", y );
		delegateSave.call( this );
	}

	function onMaximize() {
		ismaximize = true;
		setTimeout( function() {
			ismaximize = false;
		}, 20 );
	}


	function restoreWindow( nwWindow ) {
		var width  = get( this, "width" ),
		    height = get( this, "height" );
		if ( width !== null && height !== null ) {
			nwWindow.resizeTo( width, height );
		}

		var x = get( this, "x" ),
		    y = get( this, "y" );
		if ( x !== null && y !== null ) {
			nwWindow.moveTo( x, y );
		}
	}

	function resetWindow() {
		set( this, "width", null );
		set( this, "height", null );
		set( this, "x", null );
		set( this, "y", null );
		this.save();
	}


	Ember.Application.initializer({
		name: "window",
		after: [ "store", "nwjs" ],

		initialize: function( container, application ) {
			var store = container.lookup( "store:main" );

			// wait for the promise to resolve first
			application.deferReadiness();

			store.find( "window" )
				.then(function( records ) {
					return records.content.length
						? records.objectAt( 0 )
						: store.createRecord( "window", { id: 1 } ).save();
				})
				.then(function( Window ) {
					container.register( "record:window", Window, { instantiate: false } );

					// reset window
					if ( nwGui.App.fullArgv.indexOf( "--reset-window" ) >= 0 ) {
						resetWindow.call( Window );
					} else {
						restoreWindow.call( Window, nwWindow );
					}

					// also listen for the maximize events
					// we don't want to save the window size+pos after these events
					nwWindow.on(   "maximize", onMaximize );
					nwWindow.on( "unmaximize", onMaximize );

					// resize and move events need to be defered
					// the maximize events are triggered afterwards
					nwWindow.on( "resize", deferEvent( onResize.bind( Window ) ) );
					nwWindow.on(   "move", deferEvent(   onMove.bind( Window ) ) );

					// now we're ready
					application.advanceReadiness();
				});
		}<commit_msg>Rework window initializer. Fixes #62<commit_after>define( [ "nwGui", "nwWindow", "ember" ], function( nwGui, nwWindow, Ember ) {

	var get = Ember.get,
	    set = Ember.setProperties,
	    debounce = Ember.run.debounce;

	var concat = [].concat;

	var isWin = process.platform === "win32";

	var timeEvent  = 100,
	    timeIgnore = 1000,
	    ignore     = false;


	function deferEvent( thisArg, fn ) {
		var args = [ thisArg, fn ];
		return function() {
			// Ember.run.debounce( thisArg, fn, arguments..., time );
			debounce.apply( null, concat.apply( args, arguments ).concat( timeEvent ) );
		};
	}

	function save( params ) {
		set( this, params );
		this.save();
	}

	function onResize( width, height ) {
		if ( ignore ) { return; }
		save.call( this, {
			width : width,
			height: height
		});
	}

	function onMove( x, y ) {
		if ( ignore ) { return; }
		// double check: NW.js moves the window to
		// [    -8,    -8] when maximizing...
		// [-32000,-32000] when minimizing...
		if ( isWin && ( x === -8 && y === -8 || x === -32000 && x === -32000 ) ) { return; }
		save.call( this, {
			x: x,
			y: y
		});
	}

	function ignoreNextEvent() {
		ignore = true;
		debounce( unignoreNextEvent, timeIgnore );
	}

	function unignoreNextEvent() {
		ignore = false;
	}


	function restoreWindow() {
		var width  = get( this, "width" ),
		    height = get( this, "height" );
		if ( width !== null && height !== null ) {
			nwWindow.resizeTo( width, height );
		}

		var x = get( this, "x" ),
		    y = get( this, "y" );
		if ( x !== null && y !== null ) {
			nwWindow.moveTo( x, y );
		}
	}

	function resetWindow() {
		save.call( this, {
			width : null,
			height: null,
			x     : null,
			y     : null
		});
	}


	Ember.Application.initializer({
		name: "window",
		after: [ "store", "nwjs" ],

		initialize: function( container, application ) {
			var store = container.lookup( "store:main" );

			// wait for the promise to resolve first
			application.deferReadiness();

			store.find( "window" )
				.then(function( records ) {
					return records.content.length
						? records.objectAt( 0 )
						: store.createRecord( "window", { id: 1 } ).save();
				})
				.then(function( Window ) {
					container.register( "record:window", Window, { instantiate: false } );

					// reset window
					if ( nwGui.App.fullArgv.indexOf( "--reset-window" ) >= 0 ) {
						resetWindow.call( Window );
					} else {
						restoreWindow.call( Window );
					}

					// also listen for the maximize events
					// we don't want to save the window size+pos after these events
					nwWindow.on(   "maximize", ignoreNextEvent );
					nwWindow.on( "unmaximize", ignoreNextEvent );
					nwWindow.on(   "minimize", ignoreNextEvent );
					nwWindow.on(    "restore", ignoreNextEvent );

					// resize and move events need to be defered
					// the maximize events are triggered afterwards
					nwWindow.on( "resize", deferEvent( Window, onResize ) );
					nwWindow.on(   "move", deferEvent( Window, onMove   ) );

					// now we're ready
					application.advanceReadiness();
				});
		}
{'fraction_non_alphanumeric': 0.1571478626489138, 'fraction_numerical': 0.008409250175192713, 'mean_word_length': 2.6363057324840766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4644789', 'n_tokens_mistral': 2336, 'n_tokens_neox': 1879, 'n_words': 527}